package com.qingcheng.consumer;

import com.alibaba.fastjson.JSON;
import com.qingcheng.pojo.order.OrderItem;
import com.qingcheng.service.order.StockBackService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 消费类, 从消息中间件中取出消息
 *
 * @author 岳剑飞
 * @Company http://www.ithiema.com
 */
public class BackMessageConsumer implements MessageListener {

    @Autowired
    private StockBackService stockBackService;

    @Override
    public void onMessage(Message message) {

        try {
            String jsonString = new String(message.getBody());
            List<OrderItem> orderItems = JSON.parseArray(jsonString, OrderItem.class);
            //调用方法,将消息保存到回滚中间表
            stockBackService.addList(orderItems);
        } catch (Exception e) {
            e.printStackTrace();
            //记录入职
        }
    }
}
